package org.kzwl.book.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.kzwl.comm.advice.Consts;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.thear.AddThearBO;
import org.kzwl.comm.pojo.bo.thear.ThearDetails;
import org.kzwl.comm.pojo.bo.thear.ThearQueryBasicBO;
import org.kzwl.comm.pojo.dto.thear.ThearBasicDTO;
import org.kzwl.comm.utils.MyUtils;
import org.kzwl.entity.web.Thear;
import org.kzwl.service.web.ThearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app-thear")
@Api("app风采展示")
public class AppThearController {

    @Autowired
    ThearService thearService;


    @ApiOperation(value = "风采展示基础查询", notes = "只有分页参数必填，分类不必填")
    @PostMapping("/queryBasic")
    @Valid
    public ResponseData<List<ThearBasicDTO>> queryBasic(@RequestBody @Valid ThearQueryBasicBO thear) {
        IPage<Thear> page = thearService.queryBasic(thear);
        List<ThearBasicDTO> list;
        list = JSONObject.parseArray(JSONObject.toJSONString(page.getRecords()),ThearBasicDTO.class);
        list.forEach(l -> {
            l.setCuThearCenter(MyUtils.regExHtml(l.getCuThearCenter()));
            l.setCuThearCenter(l.getCuThearCenter().substring(0, Math.min(l.getCuThearCenter().length(), 30)));
            l.setCuThearUrl(Consts.MyConsts.DetailsUrl+"thear/"+l.getCuThearId());
        });
        return new ResponseData<>(list);
    }

    @ApiOperation(value = "创意投稿")
    @PostMapping("/addThear")
    @Valid
    public ResponseData<Void> addThear(@RequestBody @Valid AddThearBO bo) {
        thearService.addThear(bo);
        return new ResponseData<>();
    }
}
